Arrow (functional Programming)
   HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, arrows or bolts are a
type class In computer science, a type class is a type system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types. Such a constraint typically involves a type class T and ...
used in programming to describe
computation Computation is any type of arithmetic or non-arithmetic calculation that follows a well-defined model (e.g., an algorithm). Mechanical or electronic devices (or, historically, people) that perform computations are known as ''computers''. An es ...
s in a
pure Pure may refer to: Computing * A pure function * A pure virtual function * PureSystems, a family of computer systems introduced by IBM in 2012 * Pure Software, a company founded in 1991 by Reed Hastings to support the Purify tool * Pure-FTPd, F ...
and declarative fashion. First proposed by computer scientist John Hughes as a generalization of
monad Monad may refer to: Philosophy * Monad (philosophy), a term meaning "unit" **Monism, the concept of "one essence" in the metaphysical and theological theory ** Monad (Gnosticism), the most primal aspect of God in Gnosticism * ''Great Monad'', an ...
s, arrows provide a
referentially transparent In computer science, referential transparency and referential opacity are properties of parts of computer programs. An expression is called ''referentially transparent'' if it can be replaced with its corresponding value (and vice-versa) withou ...
way of expressing relationships between ''logical'' steps in a computation. Unlike monads, arrows don't limit steps to having one and only one input. As a result, they have found use in
functional reactive programming Functional reactive programming (FRP) is a programming paradigm for reactive programming (asynchronous dataflow programming) using the building blocks of functional programming (e.g. map, reduce, filter). FRP has been used for programming graphica ...
,
point-free programming Tacit programming, also called point-free style, is a programming paradigm in which function definitions do not identify the arguments (or "points") on which they operate. Instead the definitions merely compose other functions, among which are co ...
, and
parser Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar. The term ''parsing'' comes from Lati ...
s among other applications.


Motivation and history

While arrows were in use before being recognized as a distinct class, it wasn't until 2000 that John Hughes first published research focusing on them. Until then, monads had proven sufficient for most problems requiring the combination of program logic in pure code. However, some useful
libraries A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vir ...
, such as the Fudgets library for
graphical user interface The GUI ( "UI" by itself is still usually pronounced . or ), graphical user interface, is a form of user interface that allows users to interact with electronic devices through graphical icons and audio indicator such as primary notation, inste ...
s and certain efficient parsers, defied rewriting in a monadic form. The formal concept of arrows was developed to explain these exceptions to monadic code, and in the process, monads themselves turned out to be a
subset In mathematics, Set (mathematics), set ''A'' is a subset of a set ''B'' if all Element (mathematics), elements of ''A'' are also elements of ''B''; ''B'' is then a superset of ''A''. It is possible for ''A'' and ''B'' to be equal; if they are ...
of arrows. Since then, arrows have been an active area of research. Their underlying laws and operations have been refined several times, with recent formulations such as arrow calculus requiring only five laws.


Relation to category theory

In
category theory Category theory is a general theory of mathematical structures and their relations that was introduced by Samuel Eilenberg and Saunders Mac Lane in the middle of the 20th century in their foundational work on algebraic topology. Nowadays, cate ...
, the
Kleisli categories In category theory, a Kleisli category is a category naturally associated to any monad ''T''. It is equivalent to the category of free ''T''-algebras. The Kleisli category is one of two extremal solutions to the question ''Does every monad arise f ...
of all
monad Monad may refer to: Philosophy * Monad (philosophy), a term meaning "unit" **Monism, the concept of "one essence" in the metaphysical and theological theory ** Monad (Gnosticism), the most primal aspect of God in Gnosticism * ''Great Monad'', an ...
s form a proper subset of Hughes arrows. While Freyd categories were believed to be
equivalent Equivalence or Equivalent may refer to: Arts and entertainment *Album-equivalent unit, a measurement unit in the music industry *Equivalence class (music) *''Equivalent VIII'', or ''The Bricks'', a minimalist sculpture by Carl Andre *''Equivale ...
to arrows for a time, it has since been proven that arrows are even more general. In fact, arrows are not merely equivalent, but directly equal to enriched Freyd categories.


Definition

Like all type classes, arrows can be thought of as a set of qualities that can be applied to any
data type In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
. In the
Haskell programming language Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lang ...
, arrows allow
function Function or functionality may refer to: Computing * Function key, a type of key on computer keyboards * Function model, a structured representation of processes in a system * Function object or functor or functionoid, a concept of object-oriente ...
s (represented in Haskell by -> symbol) to combine in a reified form. However, the actual term "arrow" may also come from the fact that some (but not all) arrows correspond to the
morphism In mathematics, particularly in category theory, a morphism is a structure-preserving map from one mathematical structure to another one of the same type. The notion of morphism recurs in much of contemporary mathematics. In set theory, morphisms a ...
s (also known as "arrows" in category theory) of different Kleisli categories. As a relatively new concept, there is not a single, standard definition, but all formulations are logically equivalent, feature some required methods, and strictly obey certain mathematical laws.


Functions

The description currently used by the Haskell standard libraries ''requires'' only three basic operations: * A
type constructor In the area of mathematical logic and computer science known as type theory, a type constructor is a feature of a typed formal language that builds new types from old ones. Basic types are considered to be built using nullary type constructors. Som ...
that takes functions from any type to another , and lifts those functions into an arrow between the two types. arr (s -> t) -> A s t * A piping method that takes an arrow between two types and converts it into an arrow between
tuple In mathematics, a tuple is a finite ordered list (sequence) of elements. An -tuple is a sequence (or ordered list) of elements, where is a non-negative integer. There is only one 0-tuple, referred to as ''the empty tuple''. An -tuple is defi ...
s. The first elements in the tuples represent the portion of the input and output that is altered, while the second elements are a third type describing an unaltered portion that bypasses the computation. first (A s t) -> A (s,u) (t,u) * As all arrows must be
categories Category, plural categories, may refer to: Philosophy and general uses *Categorization, categories in cognitive science, information science and generally *Category of being *Categories (Aristotle), ''Categories'' (Aristotle) *Category (Kant) ...
, they inherit a third operation from the class of categories: A
composition Composition or Compositions may refer to: Arts and literature *Composition (dance), practice and teaching of choreography *Composition (language), in literature and rhetoric, producing a work in spoken tradition and written discourse, to include v ...
operator that can attach a second arrow to a first as long as the first function's output and the second's input have matching types. A s t >>> A t u -> A s u Although only these three procedures are strictly necessary to define an arrow, other methods can be derived to make arrows easier to work with in practice and theory. One more helpful method can be derived from and (and from which can be derived): * A merging operator that takes two arrows, possibly with different input and output types, and fuses them into one arrow between two compound types. The merge operator is ''not'' necessarily
commutative In mathematics, a binary operation is commutative if changing the order of the operands does not change the result. It is a fundamental property of many binary operations, and many mathematical proofs depend on it. Most familiar as the name o ...
. A s t *** A u v -> A (s,u) (t,v)


Arrow laws

In addition to having some well-defined procedures, arrows must obey certain rules for any types they may be applied to: * Arrows must always preserve all types' identities (essentially the definitions of all values for all types within a category). arr id

id
* When connecting two functions & , the required arrow operations must distribute over compositions from the left. arr (f >>> g)

arr f >>> arr g first (f >>> g)

first f >>> first g
* In the previous laws, piping can be applied directly to functions because order must be irrelevant when piping & lifting occur together. arr (first f)

first (arr f)
The remaining laws restrict how the piping method behaves when the order of a composition is reversed, also allowing for simplifying
expression Expression may refer to: Linguistics * Expression (linguistics), a word, phrase, or sentence * Fixed expression, a form of words with a specific meaning * Idiom, a type of fixed expression * Metaphorical expression, a particular word, phrase, o ...
s: * If an identity is merged with a second function to form an arrow, attaching it to a piped function must be commutative. arr (id *** g) >>> first f

first f >>> arr (id *** g)
* Piping a function before type simplification must be equivalent to simplifying type before connecting to the unpiped function. first f >>> arr ((s,t) -> s)

arr ((s,t) -> s) >>> f
* Finally, piping a function twice before reassociating the resulting tuple, which is nested, should be the same as reassociating the nested tuple before attaching a single bypass of the function. In other words, stacked bypasses can be flattened by first bundling together those elements unchanged by the function. first (first f) >>> arr ( ((s,t),u) -> (s,(t,u)) )

arr ( ((s,t),u) -> (s,(t,u)) ) >>> first f


Applications

Arrows may be extended to fit specific situations by defining additional operations and restrictions. Commonly used versions include arrows with choice, which allow a computation to make conditional decisions, and arrows with
feedback Feedback occurs when outputs of a system are routed back as inputs as part of a chain of cause-and-effect that forms a circuit or loop. The system can then be said to ''feed back'' into itself. The notion of cause-and-effect has to be handled ...
, which allow a step to take its own outputs as inputs. Another set of arrows, known as arrows with application, are rarely used in practice because they are actually equivalent to monads.


Utility

Arrows have several benefits, mostly stemming from their ability to make program logic explicit yet concise. Besides avoiding
side effects In medicine, a side effect is an effect, whether therapeutic or adverse, that is secondary to the one intended; although the term is predominantly employed to describe adverse effects, it can also apply to beneficial, but unintended, consequence ...
,
purely functional programming In computer science, purely functional programming usually designates a programming paradigm—a style of building the structure and elements of computer programs—that treats all computation as the evaluation of function (mathematics), mathemati ...
creates more opportunities for
static code analysis In computer science, static program analysis (or static analysis) is the analysis of computer programs performed without executing them, in contrast with dynamic program analysis, which is performed on programs during their execution. The term i ...
. This in turn can theoretically lead to better
compiler optimization In computing, an optimizing compiler is a compiler that tries to minimize or maximize some attributes of an executable computer program. Common requirements are to minimize a program's execution time, memory footprint, storage size, and power con ...
s, easier
debugging In computer programming and software development, debugging is the process of finding and resolving '' bugs'' (defects or problems that prevent correct operation) within computer programs, software, or systems. Debugging tactics can involve in ...
, and features like syntax sugar. Although no program strictly requires arrows, they generalize away much of the dense function passing that pure, declarative code would otherwise require. They can also encourage
code reuse In software development (and computer programming in general), code reuse, also called software reuse, is the use of existing software, or software knowledge, to build new software, following the reusability principles. Code reuse may be achiev ...
by giving common linkages between program steps their own class definitions. The ability to apply to types generically also contributes to reusability and keeps
interface Interface or interfacing may refer to: Academic journals * ''Interface'' (journal), by the Electrochemical Society * ''Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Linguistics'' * '' Inte ...
s simple. Arrows do have some disadvantages, including the initial effort of defining an arrow that satisfies the arrow laws. Because monads are usually easier to implement, and the extra features of arrows may be unnecessary, it is often preferable to use a monad. Another issue, which applies to many
functional programming In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declar ...
constructs, is efficiently
compiling In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
code with arrows into the imperative style used by computer
instruction set In computer science, an instruction set architecture (ISA), also called computer architecture, is an abstract model of a computer. A device that executes instructions described by that ISA, such as a central processing unit (CPU), is called an ' ...
s.


Limitations

Due to the requirement of having to define an arr function to lift pure functions, the applicability of arrows is limited. For example, bidirectional transformations cannot be arrows, because one would need to provide not only a pure function, but also its inverse, when using arr. This also limits the use of arrows to describe push-based reactive frameworks that stop unnecessary propagation. Similarly, the use of pairs to tuple values together results in a difficult coding style that requires additional combinators to re-group values, and raises fundamental questions about the equivalence of arrows grouped in different ways. These limitations remain an open problem, and extensions such as Generalized Arrows and N-ary FRP explore these problems. Much of the utility of arrows is subsumed by more general classes like
Profunctor In category theory, a branch of mathematics, profunctors are a generalization of relations and also of bimodules. Definition A profunctor (also named distributor by the French school and module by the Sydney school) \,\phi from a category C t ...
(which requires only pre- and postcomposition with functions), which have application in
optics Optics is the branch of physics that studies the behaviour and properties of light, including its interactions with matter and the construction of instruments that use or detect it. Optics usually describes the behaviour of visible, ultraviole ...
. An arrow is essentially a strong profunctor that is also a category, although the laws are slightly different.


References


External links

{{Wikibooks, Haskell, Arrows
Arrows: A General Interface to Computation


Ross Paterson, in ICFP, Sep 2001.

ghc manual Functional programming